package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exceptionhandler.BusiException;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;


    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    private CityInfoMapper cityInfoMapper;
    @Autowired
    private DistrictInfoMapper districtInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public IPage<ApartmentItemVo> getApartmentItemVoPage(IPage<ApartmentInfo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.getApartmentItemVoPage(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVoById(Long id) {

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = getById(id);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        List<GraphVo> graphVoList=graphInfoMapper.getGraphVoListByApartmentId(id);
        List<LabelInfo> labelInfoList=labelInfoMapper.getLabelInfoListByApartmentId(id);
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.getFacilityInfoListByApartmentId(id);
        List<FeeValueVo> feeValueVoList=feeValueMapper.getFeeValueVoListByApartmentId(id);

        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return apartmentDetailVo;
    }

    @Override
    public void saveOrUpdateApartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
        //  必须写在saveOrUpdate之前，否则id都有值了
        Long id = apartmentSubmitVo.getId();

        //  前端只传了省市区的id，名称需要手动查出并设置
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());

        CityInfo cityInfo = cityInfoMapper.selectById(apartmentSubmitVo.getCityId());
        apartmentSubmitVo.setCityName(cityInfo.getName());

        DistrictInfo districtInfo = districtInfoMapper.selectById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        //  多态，ApartmentSubmitVo extends ApartmentInfo，所以才能传
        //  子类特有字段若未在数据库表中定义，会被静默忽略（默认配置下)
        super.saveOrUpdate(apartmentSubmitVo);


        //  新增和修改都用这个方法，所以需要判断
        //  修改需要把旧值先删除
        if (id != null) {
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId, id));
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id));
        }

        //  如果这次是保存操作，初始时获取的id为null
        //  故执行完saveOrUpdate后应再获取一次apartmentSubmitVo.getId()

        ArrayList<ApartmentFacility> apartmentFacilityList = new ArrayList<>();
        apartmentSubmitVo.getFacilityInfoIds().forEach(facilityId -> {
            ApartmentFacility apartmentFacility = ApartmentFacility.builder().facilityId(facilityId).apartmentId(apartmentSubmitVo.getId()).build();
            apartmentFacilityList.add(apartmentFacility);
        });
        apartmentFacilityService.saveBatch(apartmentFacilityList);

        //   无论增还是改，都重新添加一遍
        ArrayList<ApartmentLabel> apartmentLabelsList = new ArrayList<>();
        apartmentSubmitVo.getLabelIds().forEach(labelId->{
            ApartmentLabel apartmentLabel = ApartmentLabel.builder().labelId(labelId).apartmentId(apartmentSubmitVo.getId()).build();
            apartmentLabelsList.add(apartmentLabel);
        });
        apartmentLabelService.saveBatch(apartmentLabelsList);

        ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
        apartmentSubmitVo.getFeeValueIds().forEach(feeValueId -> {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feeValueId).build();
            apartmentFeeValueList.add(apartmentFeeValue);
        });
        apartmentFeeValueService.saveBatch(apartmentFeeValueList);

        ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
        apartmentSubmitVo.getGraphVoList().forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfoList.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfoList);

    }

    @Override
    public void removeApartmentById(Long id) {
        long count = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        if (count > 0) {
            throw new BusiException(700, "该公寓下有房间，不能删除");
        }

        super.removeById(id);
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id));
    }
}




